/**
 *  Copyright 2011 Miguel Ángel López Vicente 
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package cc.radio.ui;

import java.util.Vector;
import cc.radio.app.Capitulo;
import cc.radio.app.Programa;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;


public class ListaCapitulos extends Activity {
	
	private Programa programa;

	 public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.listacapitulos);
	        programa = (Programa) getIntent().getSerializableExtra("programa");
	       ((TextView) findViewById(R.id.titulo)).setText(programa.getTitulo());
	        new Capitulos().execute();
	        
	  }
	
	  public void click(View v) {
	    	CapituloUI c = (CapituloUI) v.getParent();
	    	Intent data = new Intent(v.getContext(), Reproductor.class);
	    	data.putExtra("capitulo", c.getCapitulo());
	    	startActivity(data);
	  }
	  
	  public void masAntiguos(View v) {
	        new Capitulos().execute();
	  }
	    
	    
	    private class Capitulos extends AsyncTask<Void, Void, Vector<Capitulo>> {
	    	private ProgressDialog dialog;
	    	
	    	public void onPreExecute() {
	    		dialog = new ProgressDialog(ListaCapitulos.this);
	    		dialog.setTitle("Descargando...");
	    		dialog.setMessage("Obteniendo lista de capítulos...");
	    		dialog.setCancelable(false);
	    		dialog.show();
	    	}

			protected Vector<Capitulo> doInBackground(Void... a) {
				
				return programa.getCapitulos();
			}
	    	
			protected void onPostExecute(Vector<Capitulo> capitulos) {
				LinearLayout lista = (LinearLayout) findViewById(R.id.programas);			
		        for (Capitulo e : capitulos) 
		        	lista.addView(new CapituloUI(ListaCapitulos.this, e));

		        
		        dialog.dismiss();
			}
	    	
	    	
	    }

	 
}
